package com.luomo.wenyuan.modules.wenyuan.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luomo.wenyuan.common.utils.PageUtils;
import com.luomo.wenyuan.common.utils.Query;
import com.luomo.wenyuan.modules.wenyuan.dao.TPutinBookDao;
import com.luomo.wenyuan.modules.wenyuan.dao.TPutinDao;
import com.luomo.wenyuan.modules.wenyuan.entity.TPutinBookEntity;
import com.luomo.wenyuan.modules.wenyuan.entity.TPutinEntity;
import com.luomo.wenyuan.modules.wenyuan.service.TPutinService;
import com.luomo.wenyuan.modules.wenyuan.service.TStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("tPutinService")
public class TPutinServiceImpl extends ServiceImpl<TPutinDao, TPutinEntity> implements TPutinService {

    @Autowired
    private TPutinBookDao tPutinBookDao;

    @Autowired
    private TStoreService tStoreService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TPutinEntity> page = this.page(
                new Query<TPutinEntity>().getPage(params),
                new QueryWrapper<TPutinEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public String getStoreNameStrByPutinId(Integer id) {
        Set<String> storesStr = new HashSet<>();
        List<TPutinBookEntity> books = tPutinBookDao.getByPutinId(id);
        for (TPutinBookEntity book : books) {
            storesStr.add(tStoreService.getById(book.getStoreId()).getName());
        }
        return StrUtil.join(",", storesStr);
    }

}